package com.lz.hotel_java.controller;

import com.lz.hotel_java.entity.Admin;
import com.lz.hotel_java.entity.AdminReq;
import com.lz.hotel_java.mapper.AdminMapper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.Cookie;
import javax.servlet.http.HttpServletResponse;

@RestController
@RequestMapping("/admin")
@CrossOrigin("*")
public class AdminController {
    @Autowired
    private AdminMapper adminMapper;

    @PostMapping("/login")
    public String login(@RequestBody AdminReq admin, HttpServletResponse response) {
        Admin dbAdmin = adminMapper.selectByUsername(admin.getUsername());
        if (dbAdmin != null && dbAdmin.getPassword().equals(admin.getPassword())) {
            javax.servlet.http.Cookie cookie = new Cookie("admin", admin.getUsername());
            cookie.setMaxAge(24 * 60 * 60); // 1天
            cookie.setPath("/");
            response.addCookie(cookie);
            return "登录成功";
        } else {
            return "用户名或密码错误";
        }
    }
}

